<?php

	class Cwr_registrant_member_proxy extends Model
	{
		public function __construct()
		{
			parent::__construct();
		}
		
		public function getRegistrantMemberHomepageFromUrl( $urlKey, $curyear )
		{
			//Get Primary member and household info
			$getRegistrant = $this->db->query('
												SELECT    	p.person_id,
															p.person_name,
															p.password,
															p.first_name,
												          	p.last_name,
															p.gender,
															p.address1,
															p.address2,
															p.city,
															p.state,
															p.zip,
															p.email1,
															p.phone1,
															p.phone2,
															crh.display_name,
															p.volunteer_interests,
															IFNULL(p.org_name,' . $this->db->escape('') . ') AS org_name,
															IFNULL(p.secret_question,' . $this->db->escape('') . ') AS secret_question,
															IFNULL(p.secret_phrase,' . $this->db->escape('') . ') AS secret_phrase,
															cc.center_name,
															crh.type,
															crh.session,
															crh.registration_amount,
															crh.bv_amount,
															crh.donation_amount,
															crh.amount_paid
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												INNER JOIN cmb_center cc ON cc.cmb_center_id=crh.cmb_center_id_fk
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
												AND			crh.enabled = 1
												LIMIT		1;
											  ');

			$registrantHomepageValue = new Cwr_registrant_member_homepage_value();
			$registrantHomepageValue->setIsRegistered( FALSE );
			if($getRegistrant->num_rows() == 1)
			{											  
				$registrantHomepageValue->setPersonId( $getRegistrant->row()->person_id );
				$registrantHomepageValue->setUserName( $getRegistrant->row()->person_name );
				$registrantHomepageValue->setPassword( $getRegistrant->row()->password );
				$registrantHomepageValue->setFirstName( $getRegistrant->row()->first_name );
				$registrantHomepageValue->setLastName( $getRegistrant->row()->last_name );
				$registrantHomepageValue->setGender( $getRegistrant->row()->gender );
				$registrantHomepageValue->setAddress1( $getRegistrant->row()->address1 );
				$registrantHomepageValue->setAddress2( $getRegistrant->row()->address2 );
				$registrantHomepageValue->setCity( $getRegistrant->row()->city );
				$registrantHomepageValue->setState( $getRegistrant->row()->state );
				$registrantHomepageValue->setZip( $getRegistrant->row()->zip );
				$registrantHomepageValue->setEmail1( $getRegistrant->row()->email1 );
				$registrantHomepageValue->setPhone1( $getRegistrant->row()->phone1 );
				$registrantHomepageValue->setPhone2( $getRegistrant->row()->phone2 );
				$registrantHomepageValue->setDisplayName( $getRegistrant->row()->display_name );
				$registrantHomepageValue->setVolInt( $getRegistrant->row()->volunteer_interests );
				$registrantHomepageValue->setCMBCenter( $getRegistrant->row()->center_name );
				$registrantHomepageValue->setMemberType( $getRegistrant->row()->type );
				$registrantHomepageValue->setSession( $getRegistrant->row()->session );
				$registrantHomepageValue->setRegAmount( $getRegistrant->row()->registration_amount );
				$registrantHomepageValue->setBVAmount( $getRegistrant->row()->bv_amount );
				$registrantHomepageValue->setDonation( $getRegistrant->row()->donation_amount );
				$registrantHomepageValue->setAmountPaid( $getRegistrant->row()->amount_paid );
				$registrantHomepageValue->setOrgName( $getRegistrant->row()->org_name );
				$registrantHomepageValue->setSecretQuestion( $getRegistrant->row()->secret_question );
				$registrantHomepageValue->setSecretPhrase( $getRegistrant->row()->secret_phrase );
				$registrantHomepageValue->setIsRegistered( TRUE );
			}
			else
			{
				$registrantHomepageValue->setIsRegistered( FALSE );
				return $registrantHomepageValue; 
				//redirect( base_url() . 'cmb/member/homepage_error/');
			}
			
			//Get spouse info
			$getRegistrant = $this->db->query('
												SELECT    	p.person_id,
															p.first_name,
												          	p.last_name,
															p.gender,
															p.address1,
															p.address2,
															p.city,
															p.state,
															p.zip,
															p.email1,
															p.phone1,
															p.phone2,
															crh.display_name,
															crh.spousesession,
															p.volunteer_interests
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Spouse' ) . '
												AND			crh.enabled = 1
												LIMIT		1;
											  ');

			if($getRegistrant->num_rows() == 1)
			{											  
				$registrantHomepageValue->setSpousePersonId( $getRegistrant->row()->person_id );
				$registrantHomepageValue->setSpouseFirstName( $getRegistrant->row()->first_name );
				$registrantHomepageValue->setSpouseLastName( $getRegistrant->row()->last_name );
				$registrantHomepageValue->setSpouseGender( $getRegistrant->row()->gender );
				$registrantHomepageValue->setSpouseEmail1( $getRegistrant->row()->email1 );
				$registrantHomepageValue->setSpousePhone1( $getRegistrant->row()->phone1 );
				$registrantHomepageValue->setSpousePhone2( $getRegistrant->row()->phone2 );
				$registrantHomepageValue->setSpouseSession( $getRegistrant->row()->spousesession );
				$registrantHomepageValue->setSpouseVolInt( $getRegistrant->row()->volunteer_interests );
			}
			else
			{
				$registrantHomepageValue->setSpousePersonId(0);
				$registrantHomepageValue->setSpouseFirstName( "" );
				$registrantHomepageValue->setSpouseLastName( "" );
				$registrantHomepageValue->setSpouseGender( "" );
				$registrantHomepageValue->setSpouseEmail1( "" );
				$registrantHomepageValue->setSpousePhone1( "" );
				$registrantHomepageValue->setSpousePhone2( "" );
				$registrantHomepageValue->setSpouseSession( "" );
				$registrantHomepageValue->setSpouseVolInt( "" );
			}

			//Get children info
			$na = "None";
			$empty = "";
			$getRegistrant = $this->db->query('
												SELECT    	p.person_id,
															p.first_name,
												          	p.last_name,
															p.gender,
															IFNULL(p.email1,' . $this->db->escape($empty) . ') AS email1,
															IFNULL(DATE_FORMAT(p.birth_date,'.$this->db->escape("%m/%d/%Y").'),'.$this->db->escape("").') AS birth_date,
															IFNULL(brh.grade,' . $this->db->escape($na) . ') AS grade,
															IFNULL(brh.session,' . $this->db->escape($na) . ') AS session,
															IFNULL(ln.language_name,' . $this->db->escape($na) . ') AS language_name,
															IFNULL(lrh.level,' . $this->db->escape($na) . ') AS language_level,
															IFNULL(brh.enabled,0) AS enabled
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												LEFT OUTER JOIN bv_reg_history brh ON brh.person_id_fk = p.person_id AND brh.cmb_center_id_fk=crh.cmb_center_id_fk AND brh.year = crh.year AND brh.enabled = 1
												LEFT OUTER JOIN lang_reg_history lrh ON lrh.person_id_fk = p.person_id AND lrh.cmb_center_id_fk=crh.cmb_center_id_fk AND lrh.year = crh.year AND lrh.enabled = 1
												LEFT OUTER JOIN language ln ON ln.language_id = lrh.language_id_fk
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			crh.year = ' . $this->db->escape( $curyear ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Child' ) . '
												AND			crh.enabled = 1
												ORDER BY	p.person_id ASC
											  ');

			$childInfoArray = array(); //Stores attributes per child
			$childLangArray = array(); //Stores language class per child
			$childLangLevelArray = array(); //Stores language class level per child
			$childPersonIdArray = array(); //Store child person_id's
			
			if($getRegistrant->num_rows() > 0)
			{											  
				foreach ($getRegistrant->result() as $resRow)
				{			
					$childInfo = array();
					
					array_push($childPersonIdArray, $resRow->person_id);
					
					array_push($childInfo, $resRow->first_name);						
					array_push($childInfo, $resRow->last_name);						
					array_push($childInfo, $resRow->gender);						
					array_push($childInfo, $resRow->email1);
					array_push($childInfo, $resRow->birth_date);						
					array_push($childInfo, $resRow->grade);						
					array_push($childInfo, $resRow->session);
					array_push($childInfo, $resRow->enabled);
				
					array_push($childInfoArray, $childInfo);
					array_push($childLangArray, $resRow->language_name);
					array_push($childLangLevelArray, $resRow->language_level);
				}
			}
			
			$registrantHomepageValue->setChildPersonIdArray($childPersonIdArray);
			$registrantHomepageValue->setChildInfo($childInfoArray);
			$registrantHomepageValue->setChildLang($childLangArray);
			$registrantHomepageValue->setChildLangLevel($childLangLevelArray);

			return $registrantHomepageValue;
		} //getRegistrantMemberHomepageFromUrl( $urlKey )
		
		
		public function getRegistrantMemberHomepageFromUrl_noyr( $urlKey )
		{
			//Get Primary member and household info
			$getRegistrant = $this->db->query('
												SELECT    	p.person_id,
															p.person_name,
															p.password,
															p.first_name,
												          	p.last_name,
															p.gender,
															p.address1,
															p.address2,
															p.city,
															p.state,
															p.zip,
															p.email1,
															p.phone1,
															p.phone2,
															crh.display_name,
															p.volunteer_interests,
															IFNULL(p.org_name,' . $this->db->escape('') . ') AS org_name,
															IFNULL(p.secret_question,' . $this->db->escape('') . ') AS secret_question,
															IFNULL(p.secret_phrase,' . $this->db->escape('') . ') AS secret_phrase,
															cc.center_name,
															crh.type,
															crh.session,
															crh.registration_amount,
															crh.donation_amount,
															crh.amount_paid
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												INNER JOIN cmb_center cc ON cc.cmb_center_id=crh.cmb_center_id_fk
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Primary' ) . '
												AND			crh.enabled = 1
                        						GROUP BY 	p.person_id
                        						ORDER BY 	crh.year DESC
											  ');

			$registrantHomepageValue = new Cwr_registrant_member_homepage_value();
			$registrantHomepageValue->setIsRegistered( FALSE );
			if($getRegistrant->num_rows() == 1)
			{										  
				$registrantHomepageValue->setPersonId( $getRegistrant->row()->person_id );
				$registrantHomepageValue->setUserName( $getRegistrant->row()->person_name );
				$registrantHomepageValue->setPassword( $getRegistrant->row()->password );
				$registrantHomepageValue->setFirstName( $getRegistrant->row()->first_name );
				$registrantHomepageValue->setLastName( $getRegistrant->row()->last_name );
				$registrantHomepageValue->setGender( $getRegistrant->row()->gender );
				$registrantHomepageValue->setAddress1( $getRegistrant->row()->address1 );
				$registrantHomepageValue->setAddress2( $getRegistrant->row()->address2 );
				$registrantHomepageValue->setCity( $getRegistrant->row()->city );
				$registrantHomepageValue->setState( $getRegistrant->row()->state );
				$registrantHomepageValue->setZip( $getRegistrant->row()->zip );
				$registrantHomepageValue->setEmail1( $getRegistrant->row()->email1 );
				$registrantHomepageValue->setPhone1( $getRegistrant->row()->phone1 );
				$registrantHomepageValue->setPhone2( $getRegistrant->row()->phone2 );
				$registrantHomepageValue->setDisplayName( $getRegistrant->row()->display_name );
				$registrantHomepageValue->setDisplayName( $getRegistrant->row()->display_name );
				$registrantHomepageValue->setVolInt( $getRegistrant->row()->volunteer_interests );
				$registrantHomepageValue->setCMBCenter( $getRegistrant->row()->center_name );
				$registrantHomepageValue->setMemberType( $getRegistrant->row()->type );
				$registrantHomepageValue->setSession( $getRegistrant->row()->session );
				$registrantHomepageValue->setRegAmount( $getRegistrant->row()->registration_amount );
				$registrantHomepageValue->setDonation( $getRegistrant->row()->donation_amount );
				$registrantHomepageValue->setAmountPaid( $getRegistrant->row()->amount_paid );
				$registrantHomepageValue->setOrgName( $getRegistrant->row()->org_name );
				$registrantHomepageValue->setSecretQuestion( $getRegistrant->row()->secret_question );
				$registrantHomepageValue->setSecretPhrase( $getRegistrant->row()->secret_phrase );
				$registrantHomepageValue->setIsRegistered( TRUE );
			}
			else
			{				
				$registrantHomepageValue->setIsRegistered( FALSE );
				return $registrantHomepageValue; 
				//redirect( base_url() . 'cmb/member/homepage_error/');
			}
			
			//Get spouse info
			$getRegistrant = $this->db->query('
												SELECT    	p.person_id,
															p.first_name,
												          	p.last_name,
															p.gender,
															p.address1,
															p.address2,
															p.city,
															p.state,
															p.zip,
															p.email1,
															p.phone1,
															p.phone2,
															crh.display_name,
															crh.spousesession,
															p.volunteer_interests
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Spouse' ) . '
												AND			crh.enabled = 1
                        						GROUP BY 	p.person_id
                        						ORDER BY 	crh.year DESC
											  ');

			if($getRegistrant->num_rows() == 1)
			{											  
				$registrantHomepageValue->setSpousePersonId( $getRegistrant->row()->person_id );
				$registrantHomepageValue->setSpouseFirstName( $getRegistrant->row()->first_name );
				$registrantHomepageValue->setSpouseLastName( $getRegistrant->row()->last_name );
				$registrantHomepageValue->setSpouseGender( $getRegistrant->row()->gender );
				$registrantHomepageValue->setSpouseEmail1( $getRegistrant->row()->email1 );
				$registrantHomepageValue->setSpousePhone1( $getRegistrant->row()->phone1 );
				$registrantHomepageValue->setSpousePhone2( $getRegistrant->row()->phone2 );
				$registrantHomepageValue->setSpouseSession( $getRegistrant->row()->spousesession );
				$registrantHomepageValue->setSpouseVolInt( $getRegistrant->row()->volunteer_interests );
			}
			else
			{
				$registrantHomepageValue->setSpousePersonId(0);
				$registrantHomepageValue->setSpouseFirstName( "" );
				$registrantHomepageValue->setSpouseLastName( "" );
				$registrantHomepageValue->setSpouseGender( "" );
				$registrantHomepageValue->setSpouseEmail1( "" );
				$registrantHomepageValue->setSpousePhone1( "" );
				$registrantHomepageValue->setSpousePhone2( "" );
				$registrantHomepageValue->setSpouseSession( "" );
				$registrantHomepageValue->setSpouseVolInt( "" );
			}

			//Get children info
			$na = "None";
			$empty = "";
			$getRegistrant = $this->db->query('
												SELECT    	p.person_id,
															p.first_name,
												          	p.last_name,
															p.gender,
															IFNULL(p.email1,' . $this->db->escape($empty) . ') AS email1,
															IFNULL(DATE_FORMAT(p.birth_date,'.$this->db->escape("%m/%d/%Y").'),'.$this->db->escape("").') AS birth_date,
															IFNULL(brh.grade,' . $this->db->escape($na) . ') AS grade,
															IFNULL(brh.session,' . $this->db->escape($na) . ') AS session,
															IFNULL(ln.language_name,' . $this->db->escape($na) . ') AS language_name,
															IFNULL(lrh.level,' . $this->db->escape($na) . ') AS language_level,
															IFNULL(brh.enabled,0) AS enabled,
															crh.year
												FROM      	person p
												INNER JOIN household_xref hxr ON hxr.person_id_fk=p.person_id
												INNER JOIN cmb_reg_history crh ON crh.household_id_fk=hxr.household_id_fk
												LEFT OUTER JOIN bv_reg_history brh ON brh.person_id_fk = p.person_id AND brh.cmb_center_id_fk=crh.cmb_center_id_fk AND brh.year = crh.year AND brh.enabled = 1
												LEFT OUTER JOIN lang_reg_history lrh ON lrh.person_id_fk = p.person_id AND lrh.cmb_center_id_fk=crh.cmb_center_id_fk AND lrh.year = crh.year AND lrh.enabled = 1
												LEFT OUTER JOIN language ln ON ln.language_id = lrh.language_id_fk
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												AND			hxr.member_type = ' . $this->db->escape( 'Child' ) . '
												AND			crh.enabled = 1
                        						ORDER BY 	crh.year DESC, p.person_id ASC
											  ');

			$childInfoArray = array(); //Stores attributes per child
			$childLangArray = array(); //Stores language class per child
			$childLangLevelArray = array(); //Stores language class level per child
			$childPersonIdArray = array(); //Store child person_id's
			
			if($getRegistrant->num_rows() > 0)
			{											  
				$maxyear = 0;
				foreach ($getRegistrant->result() as $resRow)
				{
					if ($resRow->year > $maxyear)
						$maxyear = $resRow->year;
				}
				
				foreach ($getRegistrant->result() as $resRow)
				{			
					if ($resRow->year == $maxyear)
					{
						$childInfo = array();

						array_push($childPersonIdArray, $resRow->person_id);

						array_push($childInfo, $resRow->first_name);						
						array_push($childInfo, $resRow->last_name);						
						array_push($childInfo, $resRow->gender);						
						array_push($childInfo, $resRow->email1);						
						array_push($childInfo, $resRow->birth_date);												
						array_push($childInfo, $resRow->grade);						
						array_push($childInfo, $resRow->session);
						array_push($childInfo, $resRow->enabled);
						array_push($childInfo, $maxyear);
				
						array_push($childInfoArray, $childInfo);
						array_push($childLangArray, $resRow->language_name);
						array_push($childLangLevelArray, $resRow->language_level);
					}
				}
			}
			
			$registrantHomepageValue->setChildPersonIdArray($childPersonIdArray);
			$registrantHomepageValue->setChildInfo($childInfoArray);
			$registrantHomepageValue->setChildLang($childLangArray);
			$registrantHomepageValue->setChildLangLevel($childLangLevelArray);

			return $registrantHomepageValue;
		} //getRegistrantMemberHomepageFromUrl_noyr( $urlKey )

		public function getPersonIdFromUrl( $urlKey )
		{
			$getPersonId = $this->db->query('
												SELECT    	p.person_id
												FROM      	person p
												INNER JOIN  cwr_reg_history crh
												ON          crh.person_id_fk = p.person_id
												WHERE       crh.home_url = ' . $this->db->escape( $urlKey ) . '
												LIMIT		1;
											  ');
											  
			$personId = 0;
			
			if( $getPersonId->num_rows() > 0)
			{
				$personId = $getPersonId->row()->person_id;
			}
			
			return $personId;
		}
		
	}
?>